//
//  ZSGCViewController.m
//  数据存储
//
//  Created by Clement_Gu on 16/3/14.
//  Copyright © 2016年 小白. All rights reserved.
//

#import "ZSGCViewController.h"

@interface ZSGCViewController ()
@property (weak, nonatomic) IBOutlet UITextField *stuID;
@property (weak, nonatomic) IBOutlet UITextField *stuName;
@property (weak, nonatomic) IBOutlet UITextField *stuAge;

@property (nonatomic,strong) NSMutableArray *stuList;
@end

@implementation ZSGCViewController
/**
 */
-(NSMutableArray *)stuList
{
    if (_stuList == nil) {
        //如果之前已有存档文件，则从存档文档中读取以前的数据
        NSFileManager *fm = [NSFileManager defaultManager];
        if ([fm fileExistsAtPath:[self plistPath]]) {
            _stuList = [NSMutableArray arrayWithContentsOfFile:[self plistPath] ];
        }
        else
        {
            //没有文档则新建一个空数组
            _stuList  = [NSMutableArray array];
        
        }
    }
    return _stuList;
}
//plist文件储存路径
-(NSString *)plistPath
{
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [path stringByAppendingPathComponent:@"students.plist"];
    return filePath;
}
//保存数据到plist
-(void)savePlist
{
    [self.stuList writeToFile:[self plistPath] atomically:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//增加学生
- (IBAction)plistAddBtn:(id)sender {
    //1将信息放入字典
    NSMutableDictionary *dic = [@{@"stuId":self.stuID.text,
                                  @"stuName":self.stuName.text,
                                  @"stuAge":self.stuAge
                                  }mutableCopy];
    //2将字典放入学生数组
    [self.stuList addObject:dic];
    //3.保存一下
    [self savePlist];
    
}
- (IBAction)plistRemoveBtn:(id)sender {
    // 1. 遍历学生数组
    for(NSMutableDictionary *dict in self.stuList)
    {
        // 2. 如果学号相同
        if([dict[@"stuId"] isEqualToString:self.stuID.text])
        {
            // 3. 将该学生这个字典从数组中移除
            [self.stuList removeObject:dict];
            break;
        }
    }
    // 4. 数组保存一下
    [self savePlist];

}
- (IBAction)plistMotifyBtn:(id)sender {
    // 1. 遍历学生数组
    for(NSMutableDictionary *dict in self.stuList)
    {
        // 2. 如果学号相同
        if([dict[@"stuId"] isEqualToString:self.stuID.text])
        {
            // 3. 修改该学生的信息
            dict[@"stuName"] = self.stuName.text;
            dict[@"stuAge"] = self.stuAge.text;
            break;
        }
    }
    // 4. 数组保存一下
    [self savePlist];
}
- (IBAction)plistSearchBtn:(id)sender {
    // 1. 遍历学生数组
    for(NSMutableDictionary *dict in self.stuList)
    {
        // 2. 如果学号相同
        if([dict[@"stuId"] isEqualToString:self.stuID.text])
        {
            // 3. 将找到的这个学生信息显示到界面上
            self.stuName.text = dict[@"stuName"];
            self.stuAge.text = dict[@"stuAge"];
            break;
        }
    }

    
}
- (IBAction)plistFindAll:(id)sender {
    // 1. 遍历学生数组，显示一下
    for(NSMutableDictionary *dict in self.stuList)
    {
        NSLog(@"%@ %@ %@",dict[@"stuId"],dict[@"stuName"],dict[@"stuAge"]);
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
